using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using sfcms.api;
using System.Text;
using System.IO;

public partial class sfcms_sfcms_page : webpagebase
{
    protected override void Page_Load()
    {
        SetPageTitle("Web Page Properties");
        object[] folderAndItem = GetFolderAndItemFromQueryString(1);

        folder f = folderAndItem[0] as folder;
        page thePage = folderAndItem[1] as page;

        if (IsPostBack)
        {
            Response.Redirect(Request.Form["pageTemplate"] + "?sfcms=0&parent=" + f.id);
            return;
        }

        if (thePage != null)
        {
            Response.Redirect(thePage.friendlyurl);
            return;
        }

        StringBuilder htmlOutput = new StringBuilder();
        HandleDirectory("/", htmlOutput, 0);
        templateSelection.Text = htmlOutput.ToString();
    }

    private void HandleDirectory(string directory, StringBuilder htmlOutput, int indent)
    {
        switch (directory.ToLowerInvariant())
        {
            case "/web/sfcms/":
            case "/bin/":
                return;
        }

        // for (int i = 0; i < indent; i++) htmlOutput.Append("&nbsp;&nbsp;");
        // htmlOutput.AppendFormat("{0}<br />", directory == "/" ? directory : directory.TrimEnd('/'));

        foreach (string aspx in Directory.GetFiles(Server.MapPath(directory), "*.aspx"))
        {
            string aspxLower = aspx.ToLowerInvariant();
            if (!File.ReadAllText(aspxLower).Contains("<!-- SFCMS TEMPLATE -->")) continue;
            htmlOutput.AppendFormat("<input type=radio name=\"pageTemplate\" value=\"{0}\">{1}</a>", directory + Path.GetFileName(aspx).ToLowerInvariant(), directory + "<b>" + Path.GetFileName(aspx).ToLowerInvariant() + "</b><br />");
        }

        foreach (string directory2 in Directory.GetDirectories(Server.MapPath(directory)))
        {
            HandleDirectory("/" + directory2.Replace(Server.MapPath("/"), "").Replace("\\", "/") + "/", htmlOutput, indent + 1);
        }
    }
}
